package com.example.controller;

import com.example.common.BaseContext;
import com.example.common.Re;
import com.example.entity.ShoppingCart;
import com.example.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 购物车
 * </p>
 *
 * @author czx
 * @since 2022-04-28
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private IShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart 购物车
     * @return 返回结果
     */
    @PostMapping("/add")
    public Re<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加购物车:{}", shoppingCart);
        ShoppingCart cart = shoppingCartService.saveWithDishOrSetmeal(shoppingCart);
        if(cart!=null){
            return Re.success(cart);
        }
        return Re.error("添加购物车失败");
    }

    /**
     * 查看购物车
     *
     * @return 购物车信息
     */
    @GetMapping("/list")
    public Re<List<ShoppingCart>> list() {
        List<ShoppingCart> shoppingCarts = shoppingCartService.list();
        if (shoppingCarts != null) {
            return Re.success(shoppingCarts);
        }
        return Re.error("购物车查询失败");
    }

    /**
     * 清空购物车
     *
     * @return 结果
     */
    @DeleteMapping("/clean")
    public Re<String> clean(){
        log.info("清空购物车。。。");
        boolean clean = shoppingCartService.clean();
        if(clean){
            return Re.success("购物车清空成功");
        }
        return Re.error("购物车清空失败");
    }

    /**
     * 菜品或套餐数量-1
     *
     * @param shoppingCart 购物车
     * @return 结果
     */
    @PostMapping("/sub")
    public Re<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("商品数量减一：{}",shoppingCart);
        boolean sub = shoppingCartService.sub(shoppingCart);
        if(sub){
            return Re.success("套餐或菜品数量成功减一");
        }
        return Re.error("菜品或套餐数量减少失败");
    }
}
